package jnpf.service.impl;

import jnpf.entity.Whu_knowledgecollectionEntity;
import jnpf.mapper.Whu_knowledgecollectionMapper;
import jnpf.service.Whu_knowledgecollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.util.RandomUtil;


import jnpf.model.whu_knowledgecollection.Whu_knowledgecollectionPagination;
import java.util.stream.Collectors;





import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.text.SimpleDateFormat;
import jnpf.util.*;
import java.util.*;

/**
 *
 * whu_knowledgecollection
 * 版本： V3.2.0
 * 版权： 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * 作者： JNPF开发平台组
 * 日期： 2025-06-10
 */
@Service

public class Whu_knowledgecollectionServiceImpl extends ServiceImpl<Whu_knowledgecollectionMapper, Whu_knowledgecollectionEntity> implements Whu_knowledgecollectionService {

    @Autowired
    private UserProvider userProvider;






    @Override
    public List<Whu_knowledgecollectionEntity> getList(Whu_knowledgecollectionPagination whu_knowledgecollectionPagination){
        String userId=userProvider.get().getUserId();
        List<String> AllIdList =new ArrayList();
        int total=0;
                    QueryWrapper<Whu_knowledgecollectionEntity> whu_knowledgecollectionQueryWrapper=new QueryWrapper<>();
            int whu_knowledgecollectionNum =0;
			if(StringUtil.isNotEmpty(whu_knowledgecollectionPagination.getTodate())){
            whu_knowledgecollectionNum++;
            List<String> TodateList =  whu_knowledgecollectionPagination.getTodate();
            Long fir = Long.valueOf(TodateList.get(0));
            Long sec = Long.valueOf(TodateList.get(1));

                whu_knowledgecollectionQueryWrapper.lambda().ge(Whu_knowledgecollectionEntity::getTodate, new Date(fir))
            .le(Whu_knowledgecollectionEntity::getTodate, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

                    }

			if(StringUtil.isNotEmpty(whu_knowledgecollectionPagination.getQuestion())){
            whu_knowledgecollectionNum++;
            whu_knowledgecollectionQueryWrapper.lambda().like(Whu_knowledgecollectionEntity::getQuestion,whu_knowledgecollectionPagination.getQuestion());
                    }

			if(StringUtil.isNotEmpty(whu_knowledgecollectionPagination.getCompany())){
            whu_knowledgecollectionNum++;
                whu_knowledgecollectionQueryWrapper.lambda().eq(Whu_knowledgecollectionEntity::getCompany,whu_knowledgecollectionPagination.getCompany());
                    }

			if(StringUtil.isNotEmpty(whu_knowledgecollectionPagination.getField())){
            whu_knowledgecollectionNum++;
                whu_knowledgecollectionQueryWrapper.lambda().eq(Whu_knowledgecollectionEntity::getField,whu_knowledgecollectionPagination.getField());
                    }

			if(StringUtil.isNotEmpty(whu_knowledgecollectionPagination.getType())){
            whu_knowledgecollectionNum++;
                whu_knowledgecollectionQueryWrapper.lambda().eq(Whu_knowledgecollectionEntity::getType,whu_knowledgecollectionPagination.getType());
                    }

        if(StringUtil.isNotEmpty(whu_knowledgecollectionPagination.getQid())){
            whu_knowledgecollectionNum++;
            whu_knowledgecollectionQueryWrapper.lambda().eq(Whu_knowledgecollectionEntity::getQid,whu_knowledgecollectionPagination.getQid());
        }


        whu_knowledgecollectionQueryWrapper.lambda().eq(Whu_knowledgecollectionEntity::getUserid,userId);

        if(StringUtil.isNotEmpty(whu_knowledgecollectionPagination.getId())){
            whu_knowledgecollectionNum++;
            whu_knowledgecollectionQueryWrapper.lambda().eq(Whu_knowledgecollectionEntity::getId,whu_knowledgecollectionPagination.getId());
        }



        if(AllIdList.size()>0){
            whu_knowledgecollectionQueryWrapper.lambda().in(Whu_knowledgecollectionEntity::getId, AllIdList);
        }
        //排序
        if(StringUtil.isEmpty(whu_knowledgecollectionPagination.getSidx())){
        whu_knowledgecollectionQueryWrapper.lambda().orderByDesc(Whu_knowledgecollectionEntity::getTodate);
        }else{
        whu_knowledgecollectionQueryWrapper="asc".equals(whu_knowledgecollectionPagination.getSort().toLowerCase())?whu_knowledgecollectionQueryWrapper.orderByAsc(whu_knowledgecollectionPagination.getSidx()):whu_knowledgecollectionQueryWrapper.orderByDesc(whu_knowledgecollectionPagination.getSidx());
        }
        if((total>0 && AllIdList.size()>0) || total==0){
        Page<Whu_knowledgecollectionEntity> page=new Page<>(whu_knowledgecollectionPagination.getCurrentPage(), whu_knowledgecollectionPagination.getPageSize());
        IPage<Whu_knowledgecollectionEntity> userIPage=this.page(page, whu_knowledgecollectionQueryWrapper);
        return whu_knowledgecollectionPagination.setData(userIPage.getRecords(),userIPage.getTotal());
        }else{
            List<Whu_knowledgecollectionEntity> list = new ArrayList();
            return whu_knowledgecollectionPagination.setData(list, list.size());
        }
    }
    @Override
    public List<Whu_knowledgecollectionEntity> getTypeList(Whu_knowledgecollectionPagination whu_knowledgecollectionPagination,String dataType){
        String userId=userProvider.get().getUserId();
        List<String> AllIdList =new ArrayList();
		int total=0;
                    QueryWrapper<Whu_knowledgecollectionEntity> whu_knowledgecollectionQueryWrapper=new QueryWrapper<>();
            int whu_knowledgecollectionNum =0;
			if(StringUtil.isNotEmpty(whu_knowledgecollectionPagination.getTodate())){
            whu_knowledgecollectionNum++;
            List<String> TodateList =  whu_knowledgecollectionPagination.getTodate();
            Long fir = Long.valueOf(TodateList.get(0));
            Long sec = Long.valueOf(TodateList.get(1));

                whu_knowledgecollectionQueryWrapper.lambda().ge(Whu_knowledgecollectionEntity::getTodate, new Date(fir))
            .le(Whu_knowledgecollectionEntity::getTodate, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

                    }

			if(StringUtil.isNotEmpty(whu_knowledgecollectionPagination.getQuestion())){
            whu_knowledgecollectionNum++;
            whu_knowledgecollectionQueryWrapper.lambda().like(Whu_knowledgecollectionEntity::getQuestion,whu_knowledgecollectionPagination.getQuestion());
                    }

			if(StringUtil.isNotEmpty(whu_knowledgecollectionPagination.getCompany())){
            whu_knowledgecollectionNum++;
                whu_knowledgecollectionQueryWrapper.lambda().eq(Whu_knowledgecollectionEntity::getCompany,whu_knowledgecollectionPagination.getCompany());
                    }

			if(StringUtil.isNotEmpty(whu_knowledgecollectionPagination.getField())){
            whu_knowledgecollectionNum++;
                whu_knowledgecollectionQueryWrapper.lambda().eq(Whu_knowledgecollectionEntity::getField,whu_knowledgecollectionPagination.getField());
                    }

			if(StringUtil.isNotEmpty(whu_knowledgecollectionPagination.getType())){
            whu_knowledgecollectionNum++;
                whu_knowledgecollectionQueryWrapper.lambda().eq(Whu_knowledgecollectionEntity::getType,whu_knowledgecollectionPagination.getType());
                    }

		if(AllIdList.size()>0){
        whu_knowledgecollectionQueryWrapper.lambda().in(Whu_knowledgecollectionEntity::getId, AllIdList);
				}
        //排序
        if(StringUtil.isEmpty(whu_knowledgecollectionPagination.getSidx())){
        whu_knowledgecollectionQueryWrapper.lambda().orderByDesc(Whu_knowledgecollectionEntity::getTodate);
        }else{
        whu_knowledgecollectionQueryWrapper="asc".equals(whu_knowledgecollectionPagination.getSort().toLowerCase())?whu_knowledgecollectionQueryWrapper.orderByAsc(whu_knowledgecollectionPagination.getSidx()):whu_knowledgecollectionQueryWrapper.orderByDesc(whu_knowledgecollectionPagination.getSidx());
        }
        if("0".equals(dataType)){
            if((total>0 && AllIdList.size()>0) || total==0){
                Page<Whu_knowledgecollectionEntity> page=new Page<>(whu_knowledgecollectionPagination.getCurrentPage(), whu_knowledgecollectionPagination.getPageSize());
                IPage<Whu_knowledgecollectionEntity> userIPage=this.page(page, whu_knowledgecollectionQueryWrapper);
                return whu_knowledgecollectionPagination.setData(userIPage.getRecords(),userIPage.getTotal());
            }else{
                List<Whu_knowledgecollectionEntity> list = new ArrayList();
                return whu_knowledgecollectionPagination.setData(list, list.size());
            }
        }else{
            return this.list(whu_knowledgecollectionQueryWrapper);
        }
    }

    @Override
    public Whu_knowledgecollectionEntity getInfo(String id){
        QueryWrapper<Whu_knowledgecollectionEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(Whu_knowledgecollectionEntity::getId,id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(Whu_knowledgecollectionEntity entity){
        this.save(entity);
    }

    @Override
    public boolean update(String id, Whu_knowledgecollectionEntity entity){
        entity.setId(id);
        return this.updateById(entity);
    }
    @Override
    public void delete(Whu_knowledgecollectionEntity entity){
        if(entity!=null){
            this.removeById(entity.getId());
        }
    }
    //子表方法

    //列表子表数据方法

}
